import { _decorator, Color, Component, instantiate, Label, Node, Prefab, Sprite } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { DevelopState, Soldier } from '../entity/Soldier';
import { TrainProperty } from './TrainProperty';
import { TrainNumber } from './TrainNumber';
const { ccclass, property } = _decorator;

@ccclass('ProPanel')
export class ProPanel extends Component {
    @property(Node)
    close:Node
    @property(Node)
    trainBtn:Node
    @property(Node)
    proBtn:Node
    @property(Label)
    trainLabel:Label;
    @property(Label)
    proLabel:Label;
    @property(Prefab)
    trainNumber:Prefab
    @property(Prefab)
    trainProperty:Prefab

    private soldierData:Soldier

    private content:Node

    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.trainBtn.on(Node.EventType.TOUCH_END, this.onTrain, this)
        this.proBtn.on(Node.EventType.TOUCH_END, this.onPro, this)
    
       

        
    }

    setData(soldier:Soldier){
        this.soldierData = soldier

        this.trainLabel.string = '训练' + soldier.name
        this.proLabel.string = soldier.name + '属性'

        if(soldier.developed == DevelopState.Developed){
            this.onTrain()
        } else {
            this.onPro()
            this.trainBtn.active = false
        }   
    }

    setActive(btn:Node){
        return

        let  activeColor = new Color('#463E2D')
        let defaultColor = new Color('#1A1B15')

        this.trainBtn.getComponent(Sprite).color = defaultColor
        this.proBtn.getComponent(Sprite).color = defaultColor

        btn.getComponent(Sprite).color = activeColor
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.ProPanel)
    }

    onTrain(){
        if(this.content){
            this.content.destroy()
        }

        this.setActive(this.trainBtn)
        this.content = instantiate(this.trainNumber)
        this.node.addChild(this.content)
        this.content.getComponent(TrainNumber).setData(this.soldierData)
    }

    onPro(){
        if(this.content){
            this.content.destroy()
        }

        this.setActive(this.proBtn)
        this.content = instantiate(this.trainProperty)
        this.node.addChild(this.content)
        this.content.getComponent(TrainProperty).setData(this.soldierData)
    }
}


